package com.us.usws.biz;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

import com.us.usws.model.domain.ipodium.IpodiumUser;
import com.us.usws.model.domain.system.User;
import com.us.usws.model.flat.epserver.DOEpTokens;
import com.us.usws.model.flat.system.DOUser;
import com.us.usws.service.cud.EpTokensCUDService;
import com.us.usws.service.cud.UserLoginLogCUDService;
import com.us.usws.service.ro.EpTokensROService;
import com.us.usws.service.ro.UserLoginLogROService;
import com.us.usws.service.ro.UserROService;
import com.us.usws.support.uitls.HttpServletUtils;
import com.us.usws.support.uitls.ServiceFactory;
import com.us.usws.support.uitls.SiteWebContext;
import com.us.usws.support.uitls.WebContext;

public class LoginOutWorker {

	/**
	 * ipodium user login
	 * @param u
	 * @param swc
	 */
	public static void ipodiumLogin(DOUser u, SiteWebContext swc) {
		UserLoginLogCUDService cudService = ServiceFactory.getInstance().getService(UserLoginLogCUDService.class);
		UserLoginLogROService roService = ServiceFactory.getInstance().getService(UserLoginLogROService.class);

		//set to web context
		IpodiumUser iu = new IpodiumUser(u);
		iu.setLastLoginDate(roService.getUserLastLoginDate(u.getUid()));
		swc.setIpodiumUser(iu);

		//record login log
		String ipAddr = HttpServletUtils.getClientIpAddr(swc.getRequest());
		cudService.recordSuccessLoginLog(u.getUid(), ipAddr);

	}

	/**
	 * admin user login
	 * @param u
	 * @param wc
	 */
	public static void adminLogin(DOUser u, WebContext wc) {
		UserLoginLogCUDService cudService = ServiceFactory.getInstance().getService(UserLoginLogCUDService.class);

		//set to web context

		wc.setUser(u);

		//record login log
		String ipAddr = HttpServletUtils.getClientIpAddr(wc.getRequest());
		cudService.recordSuccessLoginLog(u.getUid(), ipAddr);

	}

	/**
	 * ep device user login
	 * @param etu
	 * @param swc
	 */
	public static void epDeviceLogin(String userUid, long tokenId) {
		EpTokensCUDService service = ServiceFactory.getInstance().getService(EpTokensCUDService.class);
		service.updateUserNo(userUid, tokenId);
	}

	public static void epDeviceLogout(long tokenId) {
		EpTokensCUDService service = ServiceFactory.getInstance().getService(EpTokensCUDService.class);
		service.updateUserNo(null, tokenId);
	}

	public static User getUserByEpIdentitiesAny(HttpServletRequest request) {
		SiteWebContext swc = new SiteWebContext(request, null);
		String uid = swc.getEpUserId();
		String token = swc.getEpToken();
		if (StringUtils.isBlank(uid) && StringUtils.isBlank(token)) {
			return null;
		}

		UserROService uS = ServiceFactory.getInstance().getService(UserROService.class);
		EpTokensROService epS = ServiceFactory.getInstance().getService(EpTokensROService.class);

		User ret;
		if (StringUtils.isBlank(uid)) {
			DOEpTokens ept = epS.getEpTokens(token);
			if (ept != null && ept.getUserNo() != null && ept.getUserNo().length() > 0) {
				uid = ept.getUserNo();
			}
		}
		if (StringUtils.isBlank(uid)) {
			return null;
		}

		ret = uS.getUser(uid);

		return ret;
	}

}
